# Build recipe for xfsprogs.
#
# Copyright (c) 2019-2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=xfsprogs
version=5.14.2
release=1

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=https://mirrors.edge.kernel.org/pub/linux/utils/fs/xfs/xfsprogs/$tarname

description="
A high-performance journaling filesystem.

XFS combines advanced journaling technology with full 64-bit addressing
and scalable structures and algorithms.  This combination delivers the
most scalable high-performance filesystem ever conceived.
"

homepage=https://xfs.org
license="GPLv2+, LGPLv2.1"

# Source documentation
docs="README VERSION LICENSES/*"
docsdir="${docdir}/${program}-${version}"

# Limit parallel jobs for this build
jobs=1

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure LDFLAGS="$QILDFLAGS" \
    OPTIMIZER="$QICFLAGS -O3" DEBUG="-DNDEBUG" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --libexecdir=/usr/libexec/xfsprogs \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-static=yes \
     --enable-shared=yes \
     --enable-blkid=yes \
     --enable-gettext=yes \
     --enable-libicu=yes \
     --enable-lto=yes \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1

    make -j${jobs} V=1 \
     DIST_ROOT="$destdir" \
     PKG_ROOT_SBIN_DIR="/usr/sbin" \
     PKG_ROOT_LIB_DIR="/usr/lib${libSuffix}" \
     install install-dev

    # Remove cron job for the experimental xfs_scrub utility
    rm -f "${destdir}/usr/lib${libSuffix}/xfsprogs/xfs_scrub_all.cron"
    rmdir "${destdir}/usr/lib${libSuffix}/xfsprogs"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation

    # Remove non-versioned location
    rm -rf "${destdir}/usr/share/doc/xfsprogs"

    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

